Imports Microsoft.VisualBasic
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System
Imports System.Data

Imports GTSDALTableAdapters
Imports GTSDAL
Imports GTSSystem.BLL.IdGenerator
Imports GTSSystem.BLL.Location
Imports GTSSystem.BLL.ContactInfo

Namespace GTSSystem.BLL
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks>
    ''' TODO: DELETE functions not implemented.
    ''' DELETE Users associated with branch > Delete Shipments > ContactINFO
    ''' Discuss
    ''' </remarks>
    <System.ComponentModel.DataObject()> _
    Public Class ClientBranch

        Private _clientBranchAdaptor As ClientBranchTableAdapter = Nothing
        Protected ReadOnly Property ClientBranchAdaptor() As ClientBranchTableAdapter
            Get
                If _clientBranchAdaptor Is Nothing Then
                    _clientBranchAdaptor = New ClientBranchTableAdapter
                End If
                Return _clientBranchAdaptor
            End Get
        End Property
        Public Sub New()
            'Constructor Code here
        End Sub
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Insert, True)> _
               Public Function CreateClientBranchBEI(ByVal BranchName As String, _
                                                  ByVal ContactName As String, _
                                                  ByVal Phone As String, _
                                                  ByVal Fax As String, _
                                                  ByVal Email As String, _
                                                  ByVal StreetAddress As String, _
                                                  ByVal City As String, _
                                                  ByVal ProvinceState As String, _
                                                  ByVal Country As String, _
                                                  ByVal PostalCode As String) As Integer

            Dim ClientBranchPK As Integer
            Dim ContactMgr As New ContactInfo
            Dim LocationMgr As New Location
            Dim LocationPK As Integer
            Dim ContactInfoPK As Integer
            Dim StatusId As Integer
            Dim ClientId As Integer
            Dim user As MembershipUser = Membership.GetUser
            Dim p As ProfileCommon = ProfileCommon.Create(user.UserName)
            StatusId = 1
            ClientId = p.ClientId

            Try
                'Add location > get location Id
                LocationPK = LocationMgr.addLocation(City, ProvinceState, Country, PostalCode)

                'Add ContactInfo > get Contactininfo id
                ContactInfoPK = ContactMgr.addContactInfo(LocationPK, ContactName, Phone, Fax, Email, StreetAddress)

                'Add Client Branch with new locationid and Contactinfoid
                ClientBranchPK = GetNextId("Clientbranch")
                If (ClientBranchPK > 0) Then
                    CreateClientBranchBEI = ClientBranchAdaptor.Insert(ClientBranchPK, BranchName, ClientId, ContactInfoPK, StatusId)
                Else
                    Throw New Exception("ClientBranch PK: Not generated")
                End If
            Catch ex As Exception
                Throw
            End Try
        End Function

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Insert, True)> _
        Public Function CreateClientBranch(ByVal ClientId As Integer, _
                                           ByVal StatusId As Integer, _
                                           ByVal BranchName As String, _
                                           ByVal ContactName As String, _
                                           ByVal Phone As String, _
                                           ByVal Fax As String, _
                                           ByVal Email As String, _
                                           ByVal StreetAddress As String, _
                                           ByVal City As String, _
                                           ByVal ProvinceState As String, _
                                           ByVal Country As String, _
                                           ByVal PostalCode As String) As Integer

            Dim ClientBranchPK As Integer
            Dim ContactMgr As New ContactInfo
            Dim LocationMgr As New Location
            Dim LocationPK As Integer
            Dim ContactInfoPK As Integer

            Try
                'Add location > get location Id
                LocationPK = LocationMgr.addLocation(City, ProvinceState, Country, PostalCode)

                'Add ContactInfo > get Contactininfo id
                ContactInfoPK = ContactMgr.addContactInfo(LocationPK, ContactName, Phone, Fax, Email, StreetAddress)

                'Add Client Branch with new locationid and Contactinfoid
                ClientBranchPK = GetNextId("Clientbranch")
                If (ClientBranchPK > 0) Then
                    CreateClientBranch = ClientBranchAdaptor.Insert(ClientBranchPK, BranchName, ClientId, ContactInfoPK, StatusId)
                Else
                    Throw New Exception("ClientBranch PK: Not generated")
                End If
            Catch ex As Exception
                Throw
            End Try
        End Function


        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Update, True)> _
        Public Function UpdateClientBranch(ByVal ClientBranchId As Integer, _
                                           ByVal ClientId As Integer, _
                                           ByVal StatusId As Integer, _
                                           ByVal ContactInfoId As Integer, _
                                           ByVal locationId As Integer, _
                                           ByVal BranchName As String, _
                                           ByVal ContactName As String, _
                                           ByVal Phone As String, _
                                           ByVal Fax As String, _
                                           ByVal Email As String, _
                                           ByVal StreetAddress As String, _
                                           ByVal City As String, _
                                           ByVal ProvinceState As String, _
                                           ByVal Country As String, _
                                           ByVal PostalCode As String) As Integer
            Dim ContactMgr As New ContactInfo
            Dim LocationMgr As New Location
            Try
                'Update location 
                LocationMgr.updateLocation(locationId, City, ProvinceState, Country, PostalCode)

                'Update ContactInfo 
                ContactMgr.updateContactInfo(ContactInfoId, locationId, ContactName, Phone, Fax, Email, StreetAddress)

                'Update Client Branch 
                If (ClientBranchId > 0) Then
                    UpdateClientBranch = ClientBranchAdaptor.Update(BranchName, ClientId, ContactInfoId, StatusId, ClientBranchId)
                Else
                    Throw New Exception("ClientBranch PK: Not Valid")
                End If
            Catch ex As Exception
                Throw
            End Try
        End Function

        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
        Public Shared Function getClientBranch(ByVal ClientiD As Integer) As ClientBranchDataTable
            Dim adapter As New ClientBranchTableAdapter
            Try
                If (ClientiD = 0) Then
                    getClientBranch = adapter.GetClientBranch
                Else
                    getClientBranch = adapter.GetClientBranchByClientID(ClientiD)
                End If

            Catch ex As Exception
                Throw
            End Try
        End Function
        <System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, False)> _
        Public Shared Function getClientBranchInfo(ByVal ClientBranchID As Integer) As ClientBranchINFODataTable
            Dim adapter As New ClientBranchINFOTableAdapter
            Try
                getClientBranchInfo = adapter.GetClientBranchINFOByClientBranchID(ClientBranchID)
            Catch ex As Exception
                Throw
            End Try
        End Function
        '<System.ComponentModel.DataObjectMethod(ComponentModel.DataObjectMethodType.Select, True)> _
        'Public Function DeleteClientBranch(ByVal ClientBranchId As Integer, ByVal Clientid As Integer)
        '    Dim ClientBranchData As New ClientBranchDataTable

        '    Try


        '    Catch ex As Exception

        '    End Try
        'End Function
    End Class
End Namespace

